package com.zheng.upms.rpc.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.JsonUtil;
import com.zheng.common.util.RedisUtil;
import com.zheng.upms.common.constant.UpmsConstant;
import com.zheng.upms.dao.mapper.UpmsDictMapper;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.dao.model.UpmsDictExample;
import com.zheng.upms.rpc.api.UpmsDictService;
import com.zheng.upms.rpc.mapper.UpmsApiMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* UpmsDictService实现
* Created by shuzheng on 2018/9/23.
*/
@Service
@Transactional
@BaseService
public class UpmsDictServiceImpl extends BaseServiceImpl<UpmsDictMapper, UpmsDict, UpmsDictExample> implements UpmsDictService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsDictServiceImpl.class);

    @Autowired
    UpmsDictMapper upmsDictMapper;

    @Autowired
    UpmsApiMapper upmsApiMapper;

    @Override
    public String queryDataDictTextByValue(String dicCode, String dicValue) {
        String dicText ="";
        List<UpmsDict> upmsDictList = queryDataDictListByDicCode(dicCode);
        if(upmsDictList!=null){
            for(int i=0;i< upmsDictList.size();i++) {
                UpmsDict upmsDict = (UpmsDict)upmsDictList.get(i);
                if(upmsDict.getDicValue().equals(dicValue)) {
                    dicText = upmsDict.getDicText();
                    break;
                }
            }
        }
        return dicText;
    }

    @Override
    public String queryDataDictValueByText(String dicCode, String dicText) {
        String dicValue ="";
        List<UpmsDict> upmsDictList = queryDataDictListByDicCode(dicCode);
        if(upmsDictList!=null){
            for(int i=0;i< upmsDictList.size();i++) {
                UpmsDict upmsDict = (UpmsDict)upmsDictList.get(i);
                if(upmsDict.getDicText().equals(dicText)) {
                    dicValue = upmsDict.getDicValue();
                    break;
                }
            }
        }
        return dicValue;
    }

    @Override
    public List<UpmsDict> queryDataDictListByDicCode(String dicCode) {
        List<UpmsDict> upmsDictList = null;
        String redisKey = UpmsConstant.REDIS_UPMS_DATA_DICT_LIST_PREFIX + ":" + dicCode;
        // 从缓存中获取数据字典的值，如果没有该值，则从数据库中获取，最后再存入redis中
        //Jedis jedis = RedisUtil.getJedis();
        String redisDdListJson = RedisUtil.get(redisKey);
        if (StringUtil.isNotEmpty(redisDdListJson)) {
            upmsDictList = JsonUtil.getObjectListFromJson(redisDdListJson, UpmsDict.class);
            return upmsDictList;
        }

        UpmsDictExample upmsDictExample = new UpmsDictExample();
        UpmsDictExample.Criteria upmsDictCriteria = upmsDictExample.createCriteria();
        upmsDictCriteria.andDicCodeEqualTo(dicCode);
        //有效
        upmsDictCriteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        upmsDictList = upmsDictMapper.selectByExample(upmsDictExample);

        // 在Redis中设置数据字典的值
        RedisUtil.set(redisKey, JsonUtil.object2Json(upmsDictList));

        return upmsDictList;
    }

    @Override
    public int cacherefresh() {
        List<UpmsDict> upmsDictList = upmsApiMapper.selectDistinctUpmsDictCode();
        if(upmsDictList!=null){
            for(int i=0;i< upmsDictList.size();i++) {
                UpmsDict upmsDict = (UpmsDict)upmsDictList.get(i);
                String redisKey = UpmsConstant.REDIS_UPMS_DATA_DICT_LIST_PREFIX + ":" + upmsDict.getDicCode();
                RedisUtil.remove(redisKey);
            }
        }
        return 1;
    }
}